package com.mm.controller.web.book;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.domain.book.DBookInfo;
import com.mm.serivce.book.BookInfoService;
import com.mm.util.result.DPage;
import com.mm.util.result.DResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description 小说
 * @Date 2023/10/8 下午4:52
 * @Author yanglin
 **/
@Api(value = "/book/info/v1/", tags = {"小说"})
@Slf4j
@RestController
@RequestMapping("/book/info/v1/")
public class BookInfoController {

    private final BookInfoService bookInfoService;

    public BookInfoController(BookInfoService bookInfoService) {
        this.bookInfoService = bookInfoService;
    }

    /**
     * 电子书分页
     *
     * @param page
     * @param limit
     * @param bookWeb
     * @param bookCategory
     * @param bookTxt
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "page", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "limit", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "bookWeb", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "int", dataTypeClass = Integer.class, name = "bookCategory", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "bookTxt", value = "")
    })
    @ApiOperation(value = "电子书分页", notes = "电子书分页", httpMethod = "POST")
    @PostMapping("/queryBookInfoPage")
    public DResult queryBookInfoPage(@RequestParam int page, @RequestParam int limit,
                                     @RequestParam(required = false) Integer bookWeb,
                                     @RequestParam(required = false) Integer bookCategory,
                                     @RequestParam(required = false) String bookTxt) {
        Page<DBookInfo> bookInfoPage = bookInfoService.queryBookInfoPage(page, limit, bookWeb, bookCategory, bookTxt);
        DPage<DBookInfo> bookInfoDPage = DPage.to(bookInfoPage);
        return DResult.of(bookInfoDPage);
    }

    /**
     * 电子书明细
     *
     * @param bookId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "bookId", value = "", required = true)
    })
    @ApiOperation(value = "电子书明细", notes = "电子书明细", httpMethod = "POST")
    @PostMapping("/queryBookInfoOne")
    public DResult<DBookInfo> queryBookInfoOne(@RequestParam Long bookId) {
        return DResult.of(bookInfoService.queryBookInfoOne(bookId));
    }

    /**
     * 电子书编辑
     *
     * @param bookInfo
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "DBookInfo", dataTypeClass = DBookInfo.class, name = "bookInfo", value = "", required = true)
    })
    @ApiOperation(value = "电子书编辑", notes = "电子书编辑", httpMethod = "POST")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DBookInfo bookInfo) {
        boolean flag = false;
        if (bookInfo.getBookId() < 0) {
            // 新增
            flag = bookInfoService.save(bookInfo);
        } else if (bookInfo.getBookId() > 0) {
            // 更新
            flag = bookInfoService.updateById(bookInfo);
        }
        return DResult.of(flag);
    }

    /**
     * 电子书删除
     *
     * @param bookId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "bookId", value = "", required = true)
    })
    @ApiOperation(value = "电子书删除", notes = "电子书删除", httpMethod = "DELETE")
    @DeleteMapping("/delete")
    public DResult delete(@RequestParam Long bookId) {
        boolean flag = false;
        if (bookId > 0) {
            flag = bookInfoService.removeBookInfo(bookId);
        }
        return DResult.of(flag);
    }

    /**
     * 同步所有小说并下载到本地
     */
    @ApiOperation(value = "同步所有小说并下载到本地", notes = "同步所有小说并下载到本地", httpMethod = "GET")
    @GetMapping("/syncAllBook")
    @Async("asyncPoolTaskExecutor")
    public void syncAllBook() {
        bookInfoService.syncAllBook();
    }

    /**
     * 同步小说并下载到本地
     *
     * @param bookId
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "bookId", value = "", required = true)
    })
    @ApiOperation(value = "同步小说并下载到本地", notes = "同步小说并下载到本地", httpMethod = "GET")
    @GetMapping("/cacheDownloadBook")
    @Async("asyncPoolTaskExecutor")
    public void cacheDownloadBook(@RequestParam Long bookId) {
        bookInfoService.syncBook(bookId);
    }

    /**
     * 下载已缓存小说到浏览器
     *
     * @param response
     * @param bookId
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "bookId", value = "", required = true)
    })
    @ApiOperation(value = "下载已缓存小说到浏览器", notes = "下载已缓存小说到浏览器", httpMethod = "GET")
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam Long bookId) {
        bookInfoService.downloadBook(response, bookId);
    }

    /**
     * 同步下载网络小说到浏览器
     *
     * @param response
     * @param bookId
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", dataTypeClass = Long.class, name = "bookId", value = "", required = true)
    })
    @ApiOperation(value = "同步下载网络小说到浏览器", notes = "同步下载网络小说到浏览器", httpMethod = "GET")
    @GetMapping("/syncDownloadBook")
    public void syncDownloadBook(HttpServletResponse response, @RequestParam Long bookId) {
        bookInfoService.syncDownloadBook(response, bookId);
    }

}
